#!/usr/bin/env python3

# this is a dbus server.

import sys
import subprocess

from PyQt5.QtCore import QCoreApplication, pyqtSlot, QObject
from PyQt5.QtDBus import QDBusConnection, QDBusInterface, QDBusReply

class Server(QObject):
    @pyqtSlot(str, result=str)
    def test_method(self, args):
        print("Get a proxy-method call... the args is: ", args)
        return 'ping("%s") got called.' % args

    @pyqtSlot()
    def test_method_without_params(self):
        print("Get a proxy-method call...")

    @pyqtSlot(str, result=bool)
    def switch_page(self, target_page: str):
        command = "zenity --info --text='切换到页面: [{}]'".format(target_page)
        subprocess.call(command, shell=True)


if __name__ == "__main__":
    app = QCoreApplication(sys.argv)
    bus = QDBusConnection.sessionBus()  # sessionBus()
    if not bus.isConnected():
        raise Exception("Fail to connect to Session-Bus.")
    # register the service
    if not bus.registerService('org.i5os.QtDBus.iodebugging'):
        raise Exception("Fail to register the service.")

    server_obj = Server()  # 创建对象 -> dbus server object
    bus.registerObject("/", 
                        # "iodebugging.test.interface",  # not in use before Qt5.5
                        server_obj,  # 创建对象 -> dbus server object
                        QDBusConnection.ExportAllSlots)  # 注册所有slot作为proxy-method
    sys.exit(app.exec_())
